﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterClients : Form
    {
        public Client ChosenClient { get; set; }

        public RegisterClients()
        {
            InitializeComponent();
            dgvClientGrid.AutoGenerateColumns = false;
            Text = Forms.CLIENT_REGISTER;
            
            btnChoose.Visible = false;
        }
        public RegisterClients(bool dict)
        {
            InitializeComponent();
            dgvClientGrid.AutoGenerateColumns = false;
            Text = Forms.CLIENT_REGISTER;

            if (dict)
            {
                btnChoose.Visible = true;
            }
            else
            {
                btnChoose.Visible = false;
            }
        }

        private void RegisterClients_Load(object sender, EventArgs e)
        {
            FillTable();
        }


        private void FillTable()
        {
            BindingSource bs = new BindingSource();
            ClientService us = new ClientService();

            IList<Client> Clients = us.getClients( Global.getFilterList(this) );

            foreach (Client Client in Clients)
                bs.Add(Client);

            dgvClientGrid.DataSource = bs;
        }


        private void dgvClientGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (btnChoose.Visible)
            {
                // jeśli wybieranie aktywne, podwójne klikmnięcie wybiera
                this.ChosenClient = (Client)dgvClientGrid.Rows[e.RowIndex].DataBoundItem;
                this.Close();
            }
            else
            if (!Global.MainPage.checkIfFormOpen(Forms.CLIENT_FORM))
            {
                FormClient fc = new FormClient((Client)dgvClientGrid.Rows[e.RowIndex].DataBoundItem);
                fc.MdiParent = Global.MainPage;
                fc.Text = Forms.CLIENT_FORM;
                fc.Show();
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            FillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (IFilter filtr in Global.getFilterList(this))
            {
                filtr.CleanUp();
            }
        }

        private void btnAddClient_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewClientForm(null);
        }

        private void btnEditClient_Click(object sender, EventArgs e)
        {
            if (dgvClientGrid.SelectedCells.Count == 1)
            {
                Global.MainPage.openNewClientForm((Client)dgvClientGrid.Rows[dgvClientGrid.SelectedCells[0].RowIndex].DataBoundItem);
                return;
            }
            
            if (dgvClientGrid.SelectedRows.Count == 1)
            {
                Global.MainPage.openNewClientForm((Client)dgvClientGrid.SelectedRows[0].DataBoundItem);
                return;
            }
            
            MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
            
        }

        private void btnDeleteClient_Click(object sender, EventArgs e)
        {
            if (dgvClientGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę zaznaczyć przynajmniej jeden wiersz do usunięcia.");
                return;
            }

            if (MessageBox.Show("Czy na pewno chcesz usunąć wszystkich zaznaczonych klientów?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                ClientService us = new ClientService();
                for (int i = 0; i < dgvClientGrid.SelectedRows.Count; i++)
                    us.deleteClient((Client)dgvClientGrid.SelectedRows[i].DataBoundItem);
                FillTable();
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            if (dgvClientGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę wybrać dokładnie jednego klienta");
            }
            else 
            {
                this.ChosenClient = (Client)dgvClientGrid.SelectedRows[0].DataBoundItem;
                this.Close();
            }
        }


    }
}
